package com.jiezuo.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiezuo.common.util.StringUtil;
import com.jiezuo.common.util.VerifyUtil;
import com.jiezuo.flowable.entity.MyFormInformation;
import com.jiezuo.flowable.entity.MyNotify;
import com.jiezuo.flowable.mapper.MyNotifyMapper;
import com.jiezuo.flowable.service.MyFormInformationService;
import com.jiezuo.flowable.service.MyNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description 动态表单知会消息
 * @author 庞留杰
 * @date 2024/09/29 11:44
 * @version V1.0
 **/
@Transactional
@Slf4j
@Service
public class MyNotifyServiceImpl extends ServiceImpl<MyNotifyMapper, MyNotify> implements MyNotifyService {

    @Autowired
    MyNotifyMapper mapper;
    @Autowired
    MyFormInformationService informationService;

    @Override
    public IPage<MyNotify> findPage(Page initMpPage, MyNotify myNotify) {
        IPage<MyNotify> page = mapper.findList(initMpPage, myNotify);
        return page;
    }

    @Override
    public List<MyNotify> findList(MyNotify myNotify) {
        List<MyNotify> list = mapper.findList(myNotify);
        return list;
    }

    @Override
    public MyNotify findById(String id) {
        return this.getById(id);
    }

    @Override
    public MyNotify viewMessage(MyNotify myNotify) {
        String id = myNotify.getId();
        VerifyUtil.notEmpty("消息ID", id);
        MyNotify byId = this.getById(id);
        byId.setIs_check("1");
        mapper.updateById(byId);
        if (StringUtil.isNotEmpty(byId.getProcess_instance_id())) {
            QueryWrapper<MyFormInformation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_delete", "0");
            queryWrapper.eq("process_instance_id", byId.getProcess_instance_id());
            List<MyFormInformation> list = informationService.list(queryWrapper);
            if (list.size() > 0) {
                MyFormInformation information = list.get(0);
                byId.setData(information.getData());
                byId.setContent(information.getContent());
            }
        }
        return byId;
    }

    @Override
    public boolean add(MyNotify myNotify) {
        return this.save(myNotify);
    }

    @Override
    public boolean update(MyNotify myNotify) {
        return this.updateById(myNotify);
    }

    @Override
    public boolean delete(String id) {
        MyNotify myNotify = new MyNotify();
        myNotify.setId(id);
        myNotify.setIs_delete("1");
        return this.updateById(myNotify);
    }

}
